package img;

import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class AgImgUtil {
    public static int target = 66;

    public static void printResult(ArrayList arr) {
        for (int i = 0; i < arr.size(); i++) {
            String rest = (String) arr.get(i);
            System.out.print("\"" + rest + "\",");
        }
        System.out.println();
    }

    public static BufferedImage getPositionBufferedImageNoSelect(BufferedImage screen, int[] position) {
        BufferedImage bit = ImgUtil.devideImg(PixelsUtil.getImageRGB(screen), position[0], position[1], position[2], position[3]);
        return bit;
    }
    public static BufferedImage getPositionBufferedImage(BufferedImage screen, int[] position) {
        BufferedImage bit = ImgUtil.devideImg(PixelsUtil.getImageRGB(screen), position[0], position[1], position[2], position[3]);
        bit = ImgUtil.selectGRB(bit, target);
        return bit;
    }

    public static String getResult(BufferedImage bit_tmp) {
        int[][] pixels_tmp = PixelsUtil.getImageRGB(bit_tmp);
        int whiteNum = 0;
        int rNum = 0;
        int gNum = 0;
        int bNum = 0;
        int thirdNum = pixels_tmp.length * pixels_tmp[0].length / 4;
        for (int w = 0; w < pixels_tmp.length; w++) {
            for (int h = 0; h < pixels_tmp[0].length; h++) {
                int pixels = pixels_tmp[w][h];
                int r = (pixels & 0xff0000) >> 16;
                int g = (pixels & 0xff00) >> 8;
                int b = (pixels & 0xff);
                if ((r == 255) && (g == 255) && (b == 255)) {
                    whiteNum++;
                } else if ((r == 255) && (g == 0) && (b == 0)) {
                    rNum++;
                } else if ((r == 0) && (g == 255) && (b == 0)) {
                    gNum++;
                } else if ((r == 0) && (g == 0) && (b == 255)) {
                    bNum++;
                }
            }
        }
        if (rNum > thirdNum) {
            return "庄";
        }
        if (gNum > thirdNum) {
            return "和";
        }
        if (bNum > thirdNum) {
            return "闲";
        }
        return null;
    }

    public static ArrayList writeSingle(BufferedImage bit) {
        //分成6*10格；j
        int w = bit.getWidth();
        int h = bit.getHeight();
        int sw = w / 10;
        int sh = h / 6;
        int num = 0;
        ArrayList arr = new ArrayList(80);
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 6; j++) {
                num++;
                //距离像素的位置。。。
                int top = sh * j;
                int bottom = sh * (5 - j);
                int left = sw * i;
                int right = sw * (9 - i);
                BufferedImage bit_tmp = ImgUtil.devideImg(PixelsUtil.getImageRGB(bit), top, left, bottom, right);
                String result = AgImgUtil.getResult(bit_tmp);
                if (result != null) arr.add(result);
                //ImgUtil.savePic(bit_tmp, path + "devided_c1_" + num + "_" + fileName);
            }
        }
        return arr;
    }

}
